package com.sunshine.lnuplus.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.widget.ImageView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import okhttp3.Response;

import static android.content.Context.MODE_PRIVATE;
import static java.lang.System.in;

public class FileHelper {

    //删除文件
    private static void deleteFile(String filePath){
        File file = new File(filePath);
        if (file.isFile() && file.exists()) {
            file.delete();
        }
    }

    public static void weatherToSp(Context context, String city, int tem, String weather, Handler handler){
        SharedPreferences wea = context.getSharedPreferences("weather", MODE_PRIVATE);
        SharedPreferences.Editor weaEditor = wea.edit();
        weaEditor.putString("city",city);
        weaEditor.putInt("tem",tem);
        weaEditor.putString("wea",weather);
        weaEditor.putInt("lastDay",TimeHelper.getDay());
        weaEditor.putInt("lastHour",TimeHelper.getHour());
        weaEditor.putInt("lastMinute", TimeHelper.getMinute());
        weaEditor.apply();
        handler.sendEmptyMessage(1);
    }

    public static void backgroundToFile(Context context, Response response, Handler handler) throws IOException {
        int month = TimeHelper.getMonth();
        int day = TimeHelper.getDay();
        //下载背景
        SharedPreferences background = context.getSharedPreferences("background", MODE_PRIVATE);
        SharedPreferences.Editor backgroundEditor = background.edit();

        InputStream in = response.body().byteStream();
        File file=new File(context.getFilesDir().getAbsolutePath() + "/"+month+"-"+ day +".jpg");
        FileOutputStream fileOutputStream=new FileOutputStream(file);
        int hasRead;
        while((hasRead=in.read())!=-1){
            fileOutputStream.write(hasRead);
        }
        fileOutputStream.close();

        //删除之前下载的旧背景
        String oldPath = context.getFilesDir().getAbsolutePath()+"/"
                +background.getInt("month",0)+"-"+background.getInt("day",0)+".jpg";
        deleteFile(oldPath);

        backgroundEditor.putInt("month",month);
        backgroundEditor.putInt("day",day);
        backgroundEditor.apply();
        System.out.println(response.body().byteStream());
        handler.sendEmptyMessage(1);
    }

    public static void setBackground(Context context, ImageView background){
        Bitmap bitmap;
        SharedPreferences getDate = context.getSharedPreferences("background",MODE_PRIVATE);
        String backgroundFileName = context.getFilesDir().getAbsolutePath() + "/"
                +getDate.getInt("month",0)+"-"+getDate.getInt("day",0)+".jpg";
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(backgroundFileName);
        } catch (FileNotFoundException ignored) {
        }
        if(fis!=null){
            bitmap = BitmapFactory.decodeStream(fis);
            background.setImageBitmap(bitmap);
        }
    }

    public static boolean setUserBackground(Context context,ImageView background){
        String fileName = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)+"/userBackground.jpg";
        Bitmap bitmap;
        try{
            FileInputStream fileInputStream = new FileInputStream(fileName);
            bitmap = BitmapFactory.decodeStream(fileInputStream);
            background.setImageBitmap(bitmap);
            return true;
        }catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static File userBackgroundToFile(File imageFile,Context context){
        try{
            if (imageFile != null && imageFile.exists()){
                imageFile.delete();
            }
            imageFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)+"/userBackground.jpg");
            FileOutputStream fileOutputStream=new FileOutputStream(imageFile);
            int hasRead;
            while((hasRead=in.read())!=-1){
                fileOutputStream.write(hasRead);
            }
            fileOutputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return imageFile;
    }

    public static void userBackgroundClear(Context context){
       deleteFile(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)+"/userBackground.jpg");
    }
}
